package com.smartinput.sync.application.service

import com.smartinput.device.application.service.DeviceService
import com.smartinput.sync.application.dto.*
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class TextSyncService(
    private val deviceService: DeviceService
) {
    
    private val logger = LoggerFactory.getLogger(TextSyncService::class.java)

    fun pushTextToDevice(
        sourceUserId: String,
        sourceDeviceId: String,
        targetDeviceId: String,
        content: String
    ): Boolean {
        // 验证权限
        if (!deviceService.validateDeviceOwnership(targetDeviceId, sourceUserId)) {
            return false
        }
        
        // 这里应该通过WebSocket推送文本到目标设备
        // 目前返回模拟结果
        return true
    }

    fun pushTextToAllUserDevices(
        userId: String,
        sourceDeviceId: String,
        content: String
    ): List<TextPushResult> {
        val userDevices = deviceService.getUserDevices(userId)
        
        return userDevices.map { device ->
            if (device.deviceId == sourceDeviceId) {
                TextPushResult(
                    deviceId = device.deviceId,
                    success = true,
                    message = "跳过源设备"
                )
            } else {
                // 这里应该通过WebSocket推送文本到设备
                TextPushResult(
                    deviceId = device.deviceId,
                    success = true,
                    message = "推送成功"
                )
            }
        }
    }

    fun notifyTextReceived(
        targetDeviceId: String,
        content: String,
        sourceDeviceId: String
    ) {
        // 这里应该通过WebSocket通知目标设备收到文本
        logger.info("通知设备 $targetDeviceId 收到来自 $sourceDeviceId 的文本: $content")
    }
} 